package com.oa.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.Contract;
import com.oa.admin.service.IContractService;
import com.oa.core.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/contract")
@Api(description = "合同表模块")
public class ContractController extends BaseController {

    private final IContractService contractService;

    @Autowired
    public ContractController(IContractService contractService) {
        this.contractService = contractService;
    }


    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<Contract> list(
            @RequestParam(required = false) String contractName,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        Wrapper wrapper = new EntityWrapper();
        if (StringUtils.isNotBlank(contractName)) {
            wrapper.like("contract_name", contractName);
        }
        Page page = new Page(pageNum, pageSize);
        return new Resp().success(new PageResult(contractService.selectPage(page, wrapper).getRecords(), page));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<Contract> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(contractService.findContractById(id));
    }


    /**
     * 新增
     *
     * @param contract Contract
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "合同表对象") @RequestBody(required = false) Contract contract) {
        if (contractService.insert(contract)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param contract Contract
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "合同表对象") @RequestBody(required = false) Contract contract) {
        if (contractService.updateById(contract)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Contract contract = contractService.selectById(id);
        if (contract != null && contract.getIsDeleted() == 2) {
            contract.setIsDeleted(1);
            if (contractService.updateById(contract)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
